package com.lijy.employee.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lijy.employee.domain.Department;
import com.lijy.employee.domain.PageBean;
import com.lijy.employee.service.DepartmentService;

@Controller
@RequestMapping("/department")
public class DepartmentController {

	@Autowired
	private DepartmentService departmentService;
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage, Model model) {
		PageBean<Department> pageBean = departmentService.findByPage(currentPage);
		model.addAttribute("pageBean", pageBean);
		return "/frame/list";
	}
	
	@RequestMapping(value="/edit/{did}", method=RequestMethod.GET)
	public String edit(@PathVariable("did") String did, Model model) {
		Department department = departmentService.findById(did);
		model.addAttribute("d", department);
		return "/frame/editDepartment";
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(Department department) {
		departmentService.update(department);
		return "redirect:/department/list";
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String addDepartment(Department department) {
		return "/frame/addDepartment";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String saveDepartment(Department department) {
		departmentService.save(department);
		return "redirect:/department/list";
	}
	
	@RequestMapping(value="/delete/{did}", method=RequestMethod.GET)
	public String deleteDepartment(@PathVariable("did") String did) {
		departmentService.delete(did);
		return "redirect:/department/list";
	}
	
}
